/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SyDeck.sdCities;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Animals;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

/**
 *
 * @author Deck /city commands
 */
public class decksMethods implements CommandExecutor {

    private final Main plugin;

    public decksMethods(Main plugin) {
        this.plugin = plugin;
    }

    public boolean onCommand(CommandSender sender, Command command, String label, String[] string) {
        String[] args = string;
        String cmd = command.getName().toLowerCase();
        Player ply = (Player) sender;
        CityInfo ci = new CityInfo();
        CityPath CP = new CityPath();
        rpgBlockListener rpgb = new rpgBlockListener();
        if (cmd.equals("city")) {
            if (args.length <= 0) {
                return false;
            } else if (args[0].equalsIgnoreCase("-rcall")) {
                if (ci.hazCity(ply.getName())) {
                    if (ply.getInventory().contains(new ItemStack(331, 4))) {
                        ply.sendMessage(ChatColor.GOLD + "Recalling....");
                        ply.teleport(ci.getloc(ci.GetCityName(ply.getName()), ply));
                        ply.getInventory().removeItem(new ItemStack(331, 4));
                    } else {
                        ply.sendMessage(ChatColor.RED + "You need at least 4 redstone ore to perform this action.");
                    }
                } else {
                    ply.sendMessage(ChatColor.RED + "You don't have a city.");
                }
            } else if (args[0].equalsIgnoreCase("create") && args.length >= 2) {
                String cityinfo = args[1].toLowerCase();
                ply.sendMessage(ChatColor.GOLD + "Creating....");
                if (!ci.izOwner(ply.getName()) && !ci.hazCity(ply.getName()) && ci.GetCityName(ply.getName()) == null) {
                    if (!rpgb.Cities.contains(cityinfo.toLowerCase()) && !ci.exCity(cityinfo.toLowerCase())) {
                        ci.MakeAccount(ply, cityinfo);
                        ply.sendMessage(ChatColor.GOLD + "City name avaiable set your limits with sponges.");
                        ply.sendMessage(ChatColor.GOLD + "And remember the sponges must be placed like a square or rectangle");
                        ItemStack sponges = new ItemStack(19);
                        sponges.setAmount(4);
                        ply.getInventory().addItem(sponges);
                    } else {
                        ply.sendMessage(ChatColor.RED + "City name already taken.");
                    }
                } else {
                    ply.sendMessage(ChatColor.RED + "You are already in a city");
                }
            } else if (args[0].equalsIgnoreCase("-q")) {
                if (ci.hazCity(ply.getName())) {
                    if (!ci.izOwner(ply.getName())) {
                        Player Paux = ply.getServer().getPlayer(ci.getCityInfo(ci.GetCityName(ply.getName())).split(",")[0]);
                        if (Paux != null) {
                            Paux.sendMessage(ChatColor.GOLD + ply.getName() + " has left your city");
                        }
                        ci.quitCity(ply.getName());
                        ply.sendMessage(ChatColor.GOLD + "You left your city, best of luck on your future adventures.");
                    } else {
                        ply.sendMessage(ChatColor.GOLD + "You own a city, try /city delete or /city GiveOwner player first.");
                    }
                } else {
                    ply.sendMessage(ChatColor.RED + "You don't have a city.");
                }
            } else {
                ply.sendMessage(ChatColor.RED + "Wrong syntax");
            }
        } else if (cmd.equalsIgnoreCase("cinfo")) {
            if (args.length <= 0) {
                return false;
            } else if (args[0].equalsIgnoreCase("-m")) {
                if (ci.hazCity(ply.getName())) {
                    String City = ci.GetCityName(ply.getName());
                    String Players = ci.getCityList(City).toString();
                    String cinfo = ci.getCityInfo(City);
                    ply.sendMessage(ChatColor.GOLD + City + " is a level " + cinfo.split(",")[1] + " city, with " + cinfo.split(",")[2] + " of " + Integer.parseInt(cinfo.split(",")[1]) * 6 + " possible population");
                    ply.sendMessage(ChatColor.GOLD + "Currently habiting the city are: " + Players);
                    ply.sendMessage(ChatColor.GOLD + "The city is owned by: " + cinfo.split(",")[0]);
                } else {
                    ply.sendMessage(ChatColor.RED + "You don't have a city.");
                }
            } else if (args[0].equalsIgnoreCase("-gi") && args.length >= 2) {
                String City = ci.GetCityName(ply.getName());
                String Players = ci.getCityList(City).toString();
                String cinfo = ci.getCityInfo(City);
                ply.sendMessage(ChatColor.GOLD + City + " is a level " + cinfo.split(",")[1] + " city, with " + cinfo.split(",")[2] + " of " + Integer.parseInt(cinfo.split(",")[1]) * 6 + " possible population");
                ply.sendMessage(ChatColor.GOLD + "Currently habiting the city are: " + Players);
                ply.sendMessage(ChatColor.GOLD + "The city is owned by: " + cinfo.split(",")[0]);
            }
        } else if (cmd.equalsIgnoreCase("cowner")) {
            if (args.length <= 0) {
                return false;
            } else if (args[0].equalsIgnoreCase("-g") && args.length >= 2) {
                if (ply.getServer().getPlayer(args[1]) != null) {
                    if (ci.izOwner(ply.getName())) {
                        String argscity = ci.GetCityName(ply.getServer().getPlayer(args[1]).getName());
                        if (argscity != null && argscity.equals(ci.GetCityName(ply.getName()))) {
                            ci.giveOwner(ply.getName(), ply.getServer().getPlayer(args[1]).getName());
                            ply.sendMessage(ChatColor.GOLD + ply.getServer().getPlayer(args[1]).getName() + " is now the city Owner.");
                            ply.getServer().getPlayer(args[1]).sendMessage(ChatColor.GOLD + ply.getName() + " made you the city Owner.");
                        } else {
                            ply.sendMessage(ChatColor.GOLD + ply.getServer().getPlayer(args[1]).getName() + " is not part of your city.");
                        }
                    } else {
                        ply.sendMessage(ChatColor.RED + "You don't own a city.");
                    }
                } else {
                    ply.sendMessage(ChatColor.RED + args[1] + " must be online.");
                }
            } else if (args[0].equalsIgnoreCase("-a") && args.length >= 2) {
                if (ply.getServer().getPlayer(args[1]) != null) {
                    if (ci.izOwner(ply.getName())) {
                        if (!ci.hazCity(ply.getServer().getPlayer(args[1]).getName())) {
                            if (ci.canAdd(ci.GetCityName(ply.getName()))) {
                                ci.addCitizen(ply.getName(), ply.getServer().getPlayer(args[1]).getName());
                                ply.sendMessage(ChatColor.GOLD + ply.getServer().getPlayer(args[1]).getName() + " added to your city");
                                ply.getServer().getPlayer(args[1]).sendMessage(ChatColor.GOLD + "You have been added to " + ci.GetCityName(ply.getName()));
                            } else {
                                ply.sendMessage(ChatColor.RED + "You have reached the population limit for your city level.");
                                ply.sendMessage(ChatColor.RED + "In order to add players you have to remove first or evolve your city.");
                            }
                        } else {
                            ply.sendMessage(ChatColor.RED + ply.getServer().getPlayer(args[1]).getName() + " already has a city.");
                        }
                    } else {
                        ply.sendMessage(ChatColor.RED + "You don't own a city.");
                    }
                } else {
                    ply.sendMessage(ChatColor.RED + args[1] + " is not online");
                }
            } else if (args[0].equalsIgnoreCase("delete")) {
                if (ci.izOwner(ply.getName())) {
                    ci.removeCity(ply);
                    ply.sendMessage(ChatColor.GOLD + "City succesfully deleted");
                } else {
                    ply.sendMessage(ChatColor.RED + "You don't own a city.");
                }
            } else if (args[0].equalsIgnoreCase("-can")) {
                String City = ci.GetCityName(ply.getName());
                if (City != null && ci.GetCityStatus(ply.getName()).equalsIgnoreCase("Building")) {
                    ci.cancelCity(ply);
                    ply.getInventory().removeItem(new ItemStack(Material.SPONGE, 99));
                    ply.sendMessage(ChatColor.GOLD + "City creation cancelled.");
                } else {
                    ply.sendMessage(ChatColor.RED + "You cannot cancel what is not created!");
                }
            } else if (args[0].equalsIgnoreCase("-b")) {
                String City = ci.GetCityName(ply.getName());
                if (City != null && ci.GetCityStatus(ply.getName()).equalsIgnoreCase("Building")) {
                    cEconomy cE = new cEconomy();
                    if (cE.pay(City, ply.getName())) {
                        rpgb.createCity(City, ply.getName());
                        ply.sendMessage(ChatColor.GOLD + "Congratulations you are the pround founder of " + City);
                    } else {
                        ply.sendMessage(ChatColor.RED + "You might need to re-check your pocket!");
                    }
                }
            } else if (args[0].equalsIgnoreCase("-r") && args.length >= 2) {
                if (ci.izOwner(ply.getName())) {
                    String Playercity = ci.GetCityName(args[1]);
                    if (Playercity != null && Playercity.equalsIgnoreCase(ci.GetCityName(ply.getName()))) {
                        ci.quitCity(args[1]);
                        ply.sendMessage(ChatColor.GOLD + args[1] + " removed from your city");
                        Player Paux = ply.getServer().getPlayer(args[1]);
                        if (Paux != null) {
                            Paux.sendMessage(ChatColor.GOLD + "You have been removed from " + ci.GetCityName(ply.getName()));
                        }
                    } else {
                        ply.sendMessage(ChatColor.RED + args[1] + " is not member of your city.");
                    }
                } else {
                    ply.sendMessage(ChatColor.RED + "You don't own a city.");
                }
            } else if (args[0].equalsIgnoreCase("-s")) {
                if (ci.izOwner(ply.getName())) {
                    String citycheck = ci.CityNameByBlock((int) ply.getLocation().getX(), (int) ply.getLocation().getZ());
                    if (citycheck != null && citycheck.equalsIgnoreCase(ci.GetCityName(ply.getName()))) {
                        if (ply.getInventory().contains(new ItemStack(331, 64))) {
                            ply.sendMessage(ChatColor.GOLD + "City Location Set!");
                            ci.setloc(ci.GetCityName(ply.getName()), ply);
                            ply.getInventory().removeItem(new ItemStack(331, 64));
                        } else {
                            ply.sendMessage(ChatColor.RED + "You need at least 64 redstone ore to perform this action.");
                        }
                    } else {
                        ply.sendMessage(ChatColor.RED + "Location is out of your city area.");
                    }
                } else {
                    ply.sendMessage(ChatColor.RED + "You don't own a city.");
                }
            }
        } else if (cmd.equalsIgnoreCase("cop")) {
            if (args.length <= 0) {
                return false;
            } else if (args[0].equalsIgnoreCase("-s") && ply.isOp()) {
                CP.SavePath();
                ply.sendMessage(ChatColor.GOLD + "Path saved!");
            } else if (args[0].equalsIgnoreCase("-g") && ply.isOp()) {
                ply.getInventory().addItem(new ItemStack(276, 1));
                ply.getInventory().setHelmet(new ItemStack(310, 1));
                ply.getInventory().setChestplate(new ItemStack(311, 1));
                ply.getInventory().setLeggings(new ItemStack(312, 1));
                ply.getInventory().setBoots(new ItemStack(313, 1));
                ply.getInventory().addItem(new ItemStack(277, 1));
                ply.getInventory().addItem(new ItemStack(278, 1));
                ply.getInventory().addItem(new ItemStack(279, 1));
                ply.getInventory().addItem(new ItemStack(259, 1));
                ply.getInventory().addItem(new ItemStack(50, 64));
                ply.getInventory().addItem(new ItemStack(320, 64));
                ply.getInventory().addItem(new ItemStack(331, 64));
                ply.setHealth(100);
            } else if (args[0].equalsIgnoreCase("-p") && ply.isOp()) {
                ply.getInventory().setLeggings(null);
                ply.getInventory().setBoots(null);
                ply.getInventory().setChestplate(null);
                ply.getInventory().setHelmet(null);
                ply.getInventory().removeItem(new ItemStack(276, 1));
                ply.getInventory().removeItem(new ItemStack(277, 1));
                ply.getInventory().removeItem(new ItemStack(278, 1));
                ply.getInventory().removeItem(new ItemStack(279, 1));
                ply.getInventory().removeItem(new ItemStack(259, 1));
                ply.getInventory().removeItem(new ItemStack(50, 64));
                ply.getInventory().removeItem(new ItemStack(320, 64));
                ply.getInventory().removeItem(new ItemStack(331, 64));
            } else if (args[0].equalsIgnoreCase("kill") && ply.isOp()) {
                killmobs(ply);
            }
        }
        else if (cmd.equalsIgnoreCase("ctodo")) { return false; }
        return true;
    }

    private void killmobs(Player ply) {
        for (LivingEntity m : ply.getWorld().getLivingEntities()) {
            if ((!isMonster(m))) {
                continue;
            }
            m.remove();
        }
    }

    public boolean isMonster(LivingEntity e) {
        return (!(e instanceof Player)) && (!(e instanceof Animals));
    }
}
